package com.starbaba.pushservice.view;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.pushservice.model.PushMessageVO;
import com.starbaba.pushservice.service.PushMessageService;

@SuppressWarnings({"serial", "rawtypes", "unchecked"})
public class ShowPushMessageAction extends BaseAction implements ModelDriven {

	private PushMessageVO model;
	private PushMessageService service;

	@Autowired
	@Qualifier("pushMessageServiceImpl")
	public void setService(PushMessageService service) {
		this.service = service;
	}


	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		service.showPage(paramsMap);
		return ActionSupport.SUCCESS;
	}
	
	public void querylist() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();
		List list = service.getList(start, length, pageResponse, paramsMap);
		pageResponse.setAaData(list);
		pageResponse.setSEcho(pageRequest.getSEcho());
		try {
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String editPage(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		service.editPage(paramsMap);
		return ActionSupport.SUCCESS;
	}

	public void deleteRecord(){
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		service.deleteRecord(paramsMap);
	}
	
	@Override
	public Object getModel() {
		if (model == null) {
			model = new PushMessageVO();
		}
		return model;
	}
}
